//
//  ViewController.m
//  车小弟模仿
//
//  Created by wangcarl on 16/8/6.
//  Copyright © 2016年 wangcarl. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    for (int i=0; i<3; i++) {
        NSString *name=[NSString stringWithFormat:@"circle%d",i+1];
        UIImage *image=[UIImage imageNamed:name];
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(0, 0, self.imageView.bounds.size.width, self.imageView.bounds.size.height);
        [button setBackgroundImage:image forState:UIControlStateNormal];
        
        [self.imageView addSubview:button];
        }
    NSString *name=@"home_btn_dealer_had_bind";
    UIImage *image=[UIImage imageNamed:name];

    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 100, 100);
    [button setBackgroundImage:image forState:UIControlStateNormal];
    button.center=CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
    [button addTarget:self action:@selector(centerButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
//    button.center=self.imageView.center;
//    button.center=CGPointMake(self.imageView.bounds.size.width/2, self.imageView.bounds.size.height/2);
//    NSLog(@"%@",NSStringFromCGPoint(button.center));
//    NSLog(@"%@",NSStringFromCGPoint(self.imageView.layer.position));
// Do any additional setup after loading the view, typically from a nib.
}
-(void)centerButton:(UIButton *)btn
{
    CGFloat curretAlpha=self.imageView.alpha;
    if (curretAlpha==1) {
        self.imageView.alpha=0;
    }else
    {
        self.imageView.alpha=1;
    }
    
    CAAnimationGroup *group=[CAAnimationGroup animation];
    
    CABasicAnimation *opacityA=[CABasicAnimation animation];
    opacityA.keyPath=@"opacity";
    
    CAKeyframeAnimation *scaleA=[CAKeyframeAnimation animation];
    scaleA.keyPath=@"transform.scale";
    
    CABasicAnimation *rotationA=[CABasicAnimation animation];
    rotationA.keyPath=@"transform.rotation";
    
    if (curretAlpha==1) {
        opacityA.fromValue=@1;
        opacityA.toValue=@0;
        scaleA.values=@[@1,@1.2,@0];
        rotationA.fromValue=@0;
        rotationA.toValue=@(-M_PI_4);
    }else
    {
        opacityA.fromValue=@0;
        opacityA.toValue=@1;
        scaleA.values=@[@0,@1.2,@1];
        rotationA.fromValue=@(-M_PI_4);
        rotationA.toValue=@0;
    }
    
    group.animations=@[opacityA,scaleA,rotationA];
    group.duration=2;
//    group.removedOnCompletion=NO;
//    group.fillMode=kCAFillModeForwards;
    [self.imageView.layer addAnimation:group forKey:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
